package analyse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;

import data.Constant;
import data.LoadData;
import data.TimeStock;

public class SinaIndustrySort {
	private static List<IndustryItem> rateSort = new ArrayList<IndustryItem>();
	public static String[] industry = {
		"生物制药",
		"金融行业",
		"有色金属",
		"仪器仪表",
		"陶瓷行业",
		"食品行业",
		"农药化肥",
		"摩托车",
		"酒店旅游",
		"家具行业",
		"环保行业",
		"供水供气",
		"服装鞋类",
		"纺织机械",
		"电子器件",
		"船舶制造",
		"其它行业",
		"综合行业",
		"造纸行业",
		"医疗器械",
		"塑料制品",
		"商业百货",
		"农林牧渔",
		"煤炭行业",
		"交通运输",
		"家电行业",
		"化纤行业",
		"飞机制造",
		"发电设备",
		"电器行业",
		"传媒娱乐",
		"房地产",
		"石油行业",
		"印刷包装",
		"物资外贸",
		"水泥行业",
		"汽车制造",
		"酿酒行业",
		"开发区",
		"建筑建材",
		"机械行业",
		"化工行业",
		"钢铁行业",
		"纺织行业",
		"电子信息",
		"电力行业",
		"玻璃行业",
		"次新股"};
	private HashMap<String, List<TimeStock>> industryStock ;
	
	public SinaIndustrySort() {
		industryStock = new HashMap<String, List<TimeStock>>();
		Collection<TimeStock> stocklist = LoadData.getStockList().values();
		for (int i = 0; i < industry.length; i++) {
			List<TimeStock> tss = new ArrayList<TimeStock>();
			industryStock.put(industry[i], tss);
			for (TimeStock ts : stocklist) {
				if(ts.getSinaIndustry().indexOf(industry[i])>=0){
					tss.add(ts);
				}
			}
		}
	}
	
	public LinkedHashSet<String> getResult(){
		rateSort.clear();
		for (int x = 0; x < industry.length; x++) {

			List<TimeStock> tlist = industryStock.get(industry[x]);
			
			if(tlist.size()<8){
				continue;
			}
			
			BigDecimal ratePercent = Constant.ZERO;
			for (TimeStock ts : tlist) {
				if(ts.getQty()==0){
					continue;
				}else{
					ratePercent = ratePercent.add(ts.getRatePercent());
				}
			}
			
			Collections.sort(tlist,new Comparator<TimeStock>() {
				@Override
				public int compare(TimeStock o1, TimeStock o2) {
					// TODO Auto-generated method stub
					return o2.getRatePercent().compareTo(o1.getRatePercent());
				}
			});
			
			IndustryItem ini = new IndustryItem();
			ini.setIndustry(industry[x]);
			ini.setAverPercent(ratePercent.divide(new BigDecimal(tlist.size()),2,BigDecimal.ROUND_HALF_UP));
			String[] codes = new String[3];
			for (int i = 0; i < 3; i++) {
				TimeStock ts1 = tlist.get(i);
//				ts1.setHead(true);
				codes[i] = ts1.getCode();
				/*if(ts1.getBeginPrice().subtract(ts1.getPreDayPrice()).multiply(Constant.ONEHUNDRED).divide(ts1.getPreDayPrice(),2,BigDecimal.ROUND_HALF_UP).compareTo(Constant.FOUR)<0){
					if(ts1.getLastStock()!=null&&ts1.getLastStock().getRatePercent().compareTo(Constant.BIGBAT)>0){
						continue;
					}
					for (int j = i+1; j < tlist.size(); j++) {
						TimeStock ts2 = tlist.get(j);
						if(ts2.getBeginPrice().subtract(ts2.getPreDayPrice()).multiply(Constant.ONEHUNDRED).divide(ts2.getPreDayPrice(),2,BigDecimal.ROUND_HALF_UP).compareTo(Constant.FOUR)<0){
							if(ts2.getLastStock()!=null&&ts1.getLastStock().getRatePercent().compareTo(Constant.BIGBAT)>0){
								continue;
							}
							String[] codes = {ts1.getCode(),ts2.getCode()};
							ini.setCode(codes);
							break;
						}
					}
					break;
				}*/
			}
			ini.setCode(codes);
			rateSort.add(ini);
		}
		
		Collections.sort(rateSort, new Comparator<IndustryItem>(){
			@Override
			public int compare(IndustryItem o1, IndustryItem o2) {
				return o2.getAverPercent().compareTo(o1.getAverPercent());
			}
		});
		LinkedHashSet<String> result =  new LinkedHashSet<String>();
		rateSort = rateSort.subList(0, 10);
		
		int index = 0;
		for (IndustryItem it : rateSort) {
			result.add(it.getCode()[0]);
			result.add(it.getCode()[1]);
			LoadData.getTimeStock(it.getCode()[0]).setSinaIndustry(it.getIndustry());
			LoadData.getTimeStock(it.getCode()[1]).setSinaIndustry(it.getIndustry());
			index++;
			if(index<11){
				LoadData.getTimeStock(it.getCode()[0]).setHead(true);
				LoadData.getTimeStock(it.getCode()[1]).setHead(true);
				LoadData.getTimeStock(it.getCode()[2]).setHead(true);
			}
		}
		
		return result;
	}
	
	public static List<IndustryItem> getRateSort(){
		return rateSort;
	}
}
